<template>
    <section class="loginWarpBg">
      <div class="loginBg"></div>
      <div class="loginWarp">
        
        <p class="title">
          <img src="/static/images/title01.png">
        </p>

        <div class="info">

          <div class="item" >
            <p><i class="icon1"></i><input type="text" v-model="name" placeholder="请输入手机号码"></p>
          </div>

          <div class="item">
            <p><i class="icon3"></i><input type="password"  v-model="password" placeholder="请输入密码"></p>
          </div>

          <div class="clickBox">
            <button class="loginA" @click="clickLogin">登 录</button>
            <router-link class="loginB" tag="button" :to="{path:'/register'}">注 册</router-link>
          </div>
          <!-- <p class="weixin"><span></span>微 信 登 录</p> -->
          <p v-if="$store.state.gender==0" class="goChoice"><router-link tag="span" :to="{path:'/choice'}">>> 先逛一逛 <<</router-link></p>
          <p v-else class="goChoice"><router-link tag="span" :to="{path:'/index'}">>> 先逛一逛 <<</router-link></p>

        </div>
      </div>
    </section>
</template>
<script>
import axios from 'axios'
import {Post} from '@/config/api.js'
export default {
  name: 'hello',
  data() {
    return {
      name: '',
      password: ''
    }
  },
  mounted(){
    let h1=$(window).height()
    $('.loginBg,.loginWarpBg').css("height", h1)
  },
  computed:{
    allCheck: function() {
      if (this.nameCheck && this.passwordCheck) {
        return true
      } else {
        return false
      }
    },
    nameCheck() {
      if (this.Input(this.name)) {
        return true
      } else {
        return false
      }
    },
    passwordCheck() {
      if (this.Input(this.password)) {
        return true
      } else {
        return false
      }
    }
  },
  methods: {
    async clickLogin() {


        let name = this.name
        let password = this.password

        let date = {
            "grant_type": "password",
            "client_id": "app",
            "client_secret": "8BAk3G10$2EWnN9Frg0SdOIovjQXXGS91gj3waZn",
            "scope": "app",
            "username": name,
            "password": password
        }
        this.$store.dispatch("loading",true)
        await Post('/token',this.$store.state.token,date).then(function(reg){
            var token = reg.data.access_token
            var arr = token.split('');
            var passToken = "";
            for (var i = 0; i < token.length; i++) {
              passToken += arr[i].charCodeAt();
              passToken += "/";
            }
            var lastToken = passToken.substring(0, passToken.length - 1)
            var d = new Date();
            d.setTime(d.getTime() + (604800000));
            var expires = "expires=" + d.toUTCString();
            document.cookie = "login" + "=" + lastToken + "; " + expires;
            this.$store.dispatch("login",token);
            //window.location.reload();
            console.log('登录成功')
            this.$router.push({ path: '/index'})
        }.bind(this)).catch((err)=>{
          let data={
            static:true,
            txt:{
              type:1,
              message:err.data.message
            }
          }
          this.$store.dispatch("alert",data)
          this.name=''
          this.password=''
        })
        this.$store.dispatch("loading",false)
        //this.$router.push({ path: '/'})
        


    },
    //关闭登录窗
    closeLogin(){
      this.$store.dispatch("closeloginBox");
    }

  }
}

</script>
